import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * This class contains the window in which the help is displayed.
 * 
 * @author Interactive Syllabus
 * 
 */

public class HelpWindow extends JFrame
{
    private static final long serialVersionUID = -7257135450358148960L;

    private HelpHandler handler;
    private JButton openButton = new JButton("Open");
    private JButton cancelButton = new JButton("Cancel");
    private JList topicList;
    private JList currentList;
    private JScrollPane topicScroller;// = new JScrollPane(topicList);;
    private JPanel buttonPanel = new JPanel();
    private JTextField searchField = new JTextField();
    private JButton searchButton = new JButton("Search");
    private JPanel searchPanel = new JPanel();
    private JButton clearButton = new JButton("Clear");

    public HelpWindow(HelpHandler handler)
    {
	super("Help");
	this.handler = handler;
	this.topicList = new JList(handler.getTopicList().toArray());
	this.currentList = this.topicList;
	this.topicList.setSelectionMode(JList.VERTICAL);

	this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	this.topicScroller = new JScrollPane(this.currentList);

	this.add(this.topicScroller, BorderLayout.NORTH);

	this.buttonPanel.add(this.openButton, BorderLayout.WEST);
	this.buttonPanel.add(this.clearButton, BorderLayout.CENTER);
	this.buttonPanel.add(this.cancelButton, BorderLayout.EAST);
	this.openButton.addActionListener(new OpenListener());
	this.cancelButton.addActionListener(new CancelListener());
	this.add(this.buttonPanel, BorderLayout.CENTER);

	this.searchField.setPreferredSize(new Dimension(100, 20));
	this.searchPanel.add(this.searchField, BorderLayout.WEST);
	this.searchPanel.add(this.searchButton, BorderLayout.EAST);

	this.searchButton.addActionListener(new SearchListener());
	this.add(this.searchPanel, BorderLayout.SOUTH);

	this.pack();
	this.setVisible(true);
	// forces the window to open in the middle of the screen
	this.setLocationRelativeTo(null);
    }

    private class OpenListener implements ActionListener
    {

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
	    // new HelpTopicDialog(this.topicList.)
	    HelpTopic selectedTopic = (HelpTopic) HelpWindow.this.topicList.getSelectedValue();
	    if (selectedTopic != null)
	    {
		new HelpTopicDialog(selectedTopic);
	    }
	}

    }

    private class CancelListener implements ActionListener
    {

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
	    HelpWindow.this.dispose();

	}

    }

    public void displayResults()
    {
	this.currentList = new JList(this.handler.getHelp(this.searchField.getText()).toArray());
	this.topicScroller.setViewportView(this.currentList);

	/*
	 * this.topicList = new JList(this.handler.getHelp(this.searchField.getText()).toArray()); this.topicScroller = new JScrollPane(this.topicList); this.add(topicScroller, BorderLayout.NORTH); this.topicScroller.repaint();
	 */
    }

    public void clearResults()
    {

	this.currentList = this.topicList;
	this.topicScroller.setViewportView(this.currentList);

    }

    private class SearchListener implements ActionListener
    {

	@Override
	public void actionPerformed(ActionEvent arg0)
	{

	    HelpWindow.this.displayResults();

	}

    }

//    private class ClearListener implements ActionListener
//    {
//
//	@Override
//	public void actionPerformed(ActionEvent arg0)
//	{
//
//	    HelpWindow.this.clearResults();
//	}
//
//    }

}
